setwd("C:/Users/eas24f/OneDrive - Florida State University/Research/Inertia RESTAT/Data") # Office computer directory

output_for_ashley <- FALSE

if(output_for_ashley) {
	choices <- get(load("ca_choice_matrix_AUG032019_ashley"))
	previous_choices <- get(load("ca_previous_choice_matrix_AUG032019_ashley"))	
	new_choices <- get(load("ca_new_choice_matrix_AUG032019_ashley"))
	premiums <- get(load("ca_premium_matrix_AUG032019_ashley"))
	avs <- get(load("ca_AV_matrix_AUG032019_ashley"))
	subsidies <- get(load("ca_subsidy_matrix_AUG032019_ashley"))
	households <- get(load("ca_household_data_for_ashley"))
} else {
	choices <- get(load("ca_choice_matrix_AUG032019_churn"))
	previous_choices <- get(load("ca_previous_choice_matrix_AUG032019_churn"))	
	new_choices <- get(load("ca_new_choice_matrix_AUG032019_churn"))
	premiums <- get(load("ca_premium_matrix_AUG032019_churn"))
	avs <- get(load("ca_AV_matrix_AUG032019_churn"))
	subsidies <- get(load("ca_subsidy_matrix_AUG032019_churn"))
	households <- get(load("ca_household_characteristics_AUG032019_churn"))
}

##### Initialize output object

	valid_choices <- as.vector(!is.na(t(choices)))	
	julia_output <- expand.grid(colnames(choices),1:nrow(choices))[valid_choices,]
	gc()
	
##### Save year and weight from household object and then delete
	
	household_years <- rep(households[rownames(choices),"year"],each=ncol(choices))[valid_choices]	
	weight <- rep(households[rownames(choices),"weight"],each=ncol(choices))[valid_choices]
	penalties <- rep(households[rownames(choices),"penalty"]/12,each=ncol(choices))[valid_choices]
	subsidies <- as.vector(t(subsidies))[valid_choices]
	#rm(households)
	gc()
		
##### Reduce memory of premium and choice objects
	
	num_households <- nrow(choices)
	num_plans <- ncol(choices)
	
	# Reduce memory
	choices <- as.vector(t(choices))[valid_choices]
	gc()
	
	previous_choices <- as.vector(t(previous_choices))[valid_choices]
	gc()
	
	new_choices <- as.vector(t(new_choices))[valid_choices]
	gc()
	
	if(output_for_ashley) {
		premiums <- as.vector(t(premiums))[valid_choices] + subsidies
	} else {
		premiums <- as.vector(t(premiums))[valid_choices] - penalties
	}
	gc()
	
	avs <- as.vector(t(avs))[valid_choices]
	
	gc()
	
##### To send to Julia
	
	julia_output$choice <- choices
	rm(choices)
	gc()
	
	julia_output$previous_choice <- previous_choices
	julia_output[is.na(julia_output$previous_choice),"previous_choice"] <- 0
	
	julia_output$new_plan <- new_choices
	julia_output$premium <- premiums
	julia_output$av <- avs
	julia_output$subsidy <- subsidies
	julia_output$penalty <- penalties
	rm(new_choices)
	rm(premiums)
	rm(avs)
	rm(subsidies)
	rm(penalties)
	gc()
	
	julia_output$year <- household_years
	rm(household_years)
	gc()
	
	julia_output$weight <- weight
	rm(weight)
	gc()
		
	julia_output$uninsured_plan <- rep(c(rep(0,num_plans-1),1),num_households)[valid_choices]
	julia_output[julia_output$uninsured_plan == 1,c("premium","subsidy")] <- 0
	
	##### Create Choice Set Identifier
	# We want to identify all households with the same choice set
	# This varies within rating area because of heterogeneous firm entry
	
	households$choice_set_name <- paste(households$rating_area,households$year,households$num_choices,sep="_")
	
		# This gives the unique choice set name for everything except 2 zip-region-years ("956_1_2016","957_1_2016")
		households[households$zip_region_year == "957_1_2016","choice_set_name"] <- "1_2016_16_2"
	
		# One other issue with zip_region_year 958_1_2015
		households[households$zip_region_year == "958_1_2015","choice_set_name"] <- "1_2015_8_2"
		
	
	unique_choice_sets <- households[!duplicated(households$choice_set_name),c("choice_set_name","num_choices","year")]
	households$choice_set_id <- NA
	for(j in 1:nrow(unique_choice_sets)) {
		households[households$choice_set_name == unique_choice_sets[j,"choice_set_name"],"choice_set_id"] <- j
	}
	
	colnames(julia_output) <- c("plan_name","household_number","choice","previous_choice","new_plan",
		"premium","av","subsidy","penalty","year","weight","uninsured_plan")
	julia_output$choice_set_id <- households[julia_output$household_number,"choice_set_id"]
	julia_output$household_id <- households[julia_output$household_number,"household_id"]
	
	if(output_for_ashley) {
		write.csv(julia_output,file="ca_choice_data_for_ashley.csv",row.names=FALSE)		
	} else {
		write.csv(julia_output,file="ca_julia_data_AUG032019_churn.csv",row.names=FALSE)		
		write.csv(unique_choice_sets,file="unique_choice_sets_MAY112021_churn.csv",row.names=FALSE)
	}

